package com.hexinfo.dmpro.dev.data.transfer.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hexinfo.dmpro.common.base.PageResult;
import com.hexinfo.dmpro.dev.data.transfer.model.dto.FieldListDto;
import com.hexinfo.dmpro.dev.data.transfer.service.IDesensitizeFieldListService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * @Package: com.hex.ds.hdrs.common.excel
 * @ClassName TalentImportVerifyHandler
 * @Description: 周期作业定义导入自定义校验
 * @Author: ji.tang
 * @Date 2023/2/28 11:08
 * @Version v1.0
 **/
@Data
@Component
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class FieldListHandler implements IExcelVerifyHandler<FieldListDto> {


    private final ThreadLocal<List<FieldListDto>> threadLocal = new ThreadLocal<>();


    private final IDesensitizeFieldListService fieldListService;

    /*
     * @Method: verifyHandler <br>
     * @Param: [FieldListDto] <br>
     * @Return: cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult <br>
     * @Description：导入校验<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/3 11:46 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public ExcelVerifyHandlerResult verifyHandler(FieldListDto fieldListDto) {
        if(fieldListDto.isEmpty()) return new ExcelVerifyHandlerResult(true);
        List<FieldListDto> threadLocalVal = threadLocal.get();
        if (threadLocalVal == null) {
            threadLocalVal = new ArrayList<>();
        }
        StringJoiner joiner = new StringJoiner(",");
        // 任务编码Excel数据重复校验和DB数据库数据校验
        checkTaskCode(joiner, fieldListDto, threadLocalVal);

        threadLocalVal.add(fieldListDto);
        threadLocal.set(threadLocalVal);
        if (joiner.length() != 0) {
            return new ExcelVerifyHandlerResult(false, joiner.toString());
        }
        return new ExcelVerifyHandlerResult(true);
    }

    /*
     * @Method: checkTaskCode <br>
     * @Param: [joiner, FieldListDto, threadLocalVal] <br>
     * @Return: void <br>
     * @Description：任务编码校验<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/4 15:43 <br>
     * @Version： V2.0.2<br>
     */
    public void checkTaskCode(StringJoiner joiner, FieldListDto fieldListDto, List<FieldListDto> threadLocalVal) {
        for (FieldListDto haveCheckDto : threadLocalVal) {
            if (haveCheckDto.getFullField().equals(fieldListDto.getFullField())){
                joiner.add("[脱敏字段配置]" + fieldListDto.getFullField() + "与第" + (haveCheckDto.getRowNum() + 1) + "行数据重复");
            }
        }
        PageResult result = fieldListService.queryList(new Page<>(),fieldListDto.toDesensitizeFieldList());
        if(result.getTotalElements()!=0l) {
            joiner.add("[脱敏字段配置]" + fieldListDto.getFullField() + "已存在，不允许重复新增");
        }
    }
}
